package ug.co.icom.kyazze.healthticket;

import ug.co.icom.kyazze.healthticket.data.EmailReturnValue;
import ug.co.icom.kyazze.healthticket.email.EmailValidator;
import ug.co.icom.kyazze.healthticket.email.MailService;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ForgotPasswordActivity extends Activity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forgotpasswordactivity_layout);

		Button recoverButton = (Button) findViewById(R.id.button1);
		recoverButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		try
		{
			if(v.getId() == R.id.button1)
			{
				EditText emailEditText = (EditText) findViewById(R.id.editText1);
				String email = emailEditText.getText().toString();
				if(!email.equals(""))
				{
					if(emailIsValid(email))
					{
						MailService ms = new MailService(this);
						EmailReturnValue erv = ms.execute(email).get();
						if(erv.retVal)
						{
							AlertDialog.Builder builder = new AlertDialog.Builder(ForgotPasswordActivity.this);
							builder.setMessage("Your login details have been sent to your email");
							builder.setCancelable(true);
							builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog, int which) {
									dialog.cancel();

									finish();
								}
							});
							AlertDialog alert = builder.create();
							alert.show();
						}
						else
						{
							AlertDialog.Builder builder = new AlertDialog.Builder(ForgotPasswordActivity.this);
							builder.setMessage(erv.Message);
							builder.setCancelable(true);
							builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog, int which) {
									dialog.cancel();

									//finish();
								}
							});
							AlertDialog alert = builder.create();
							alert.show();
						}
					}
					else
					{
						AlertDialog.Builder builder = new AlertDialog.Builder(ForgotPasswordActivity.this);
						builder.setMessage("Please provide a valid Email Address");
						builder.setCancelable(true);
						builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.cancel();
							}
						});
						AlertDialog alert = builder.create();
						alert.show();
					}
				}
				else
				{
					AlertDialog.Builder builder = new AlertDialog.Builder(ForgotPasswordActivity.this);
					builder.setMessage("Please enter an email");
					builder.setCancelable(true);
					builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					});
					AlertDialog alert = builder.create();
					alert.show();
				}
			}
		}
		catch(Exception e)
		{

		}
	}

	private Boolean emailIsValid(String email)
	{
		EmailValidator ev = new EmailValidator();
		return ev.validateEmail(email);
	}
}
